import CommonModel from "@/components/common/CommonModel";
import React, {useImperativeHandle, forwardRef, useRef} from "react";

interface IModelRef {
    showModal: () => void
    handleCancel: (cb?: () => void) => void
    handleOk: (cb?: () => void) => void
}

interface IProps {
    btn: React.ReactNode
    content: React.ReactNode
    footer: React.ReactNode
    title: string
}

const EditModal = forwardRef(({ btn, title, content, footer }: IProps, ref) => {
    const modalRef = useRef(null)
    const handleCancel = () => {
        if (modalRef.current) {
            (modalRef.current as IModelRef).handleCancel()
        }
    }
    const handleOk = (cb?: () => void) => {
        if (modalRef.current) {
            if(cb) {
              cb()
            };
            (modalRef.current as IModelRef).handleOk()
        }
    }
    const showModal = () => {
        if (modalRef.current) {
            (modalRef.current as IModelRef).showModal()
        }
    }
    useImperativeHandle(ref, () => ({ showModal, handleOk, handleCancel }))
    return (
        <CommonModel
            close={handleCancel}
            footer={footer}
            ref={modalRef}
            title={title}
            btn={btn}
            content={content}
        />
    )
})

export default EditModal